package com.wanhelibs;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.squareup.leakcanary.LeakCanary;
import com.wanhe.base.BaseApplication;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.base.util.StringFileUtil;
import com.wanhe.mealbookmodule.AppConf;
import com.wanhe.mealbookmodule.GeneratorInfo;
import com.wanhe.viewconf.ViewCreator;
import com.wanhe.viewconf.generator.IViewGenerator;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 *
 */

public class MainApplication extends BaseApplication {

	public static final String	TAG	= "MainApplication";

	private AppConf				appConf;

	@Override
	public void onCreate() {
		super.onCreate();

		initViewCreator();

		LeakCanary.install(this);

	}

	private void initViewCreator() {
		InputStream open;
		try {
			open = getApplicationContext().getAssets().open("appConfig.json");
			String viewConfigJson = StringFileUtil.convertStreamToString(open);
			appConf = new Gson().fromJson(viewConfigJson, AppConf.class);

			ViewCreator.ViewConfig config = ViewCreator.ViewConfig.builder()
					.counterWithIMG(appConf.isPlusText)
					.couterImg(
							getResources()
									.getDrawable(com.wanhe.mealbookmodule.R.drawable.plus_nor_bg),
							getResources()
									.getDrawable(com.wanhe.mealbookmodule.R.drawable.reduce_nor_bg))
					.salloutImg(getResources()
							.getDrawable(com.wanhe.mealbookmodule.R.drawable.sall_out_light))
					.extentionPT(1.0f)
					.txtFontPath(appConf.com_tf)
					.txtFontSizePT(10)
					.counterIconTypeface(appConf.counter_tf)
					.counterIconFontSizePt(10)
					.init(this.initMapData)
					.isDebug(BuildConfig.DEBUG)
					.build();
			ViewCreator.getInstance().setContext(getApplicationContext()).init(config);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 *
	 * 初始化控件加载映射的map
	 *
	 * @author 陈德基
	 * @time 2017/7/10 10:25
	 */
	private ViewCreator.ViewConfig.Builder.OnInitMapData initMapData = new ViewCreator.ViewConfig.Builder.OnInitMapData() {
		@Override
		public void initMap(Map<String, String> map) {
			List<GeneratorInfo> viewConfigs = appConf.viewConfigs;
			if (ObjectUtil.checkObjectList(viewConfigs)) {
				for (int i = 0; i < viewConfigs.size(); i++) {
					String key = viewConfigs.get(i).key;
					String gen = viewConfigs.get(i).gen;
					map.put(key, gen);
					Logger.t(TAG).i("key: %s, gen: %s", key, gen);
				}
			}
		}

		@Override
		public void extentionGenerator(String genName, Map<String, IViewGenerator> generatorMap) {

			/**
			 * 分支版本的扩展控件的加载
			 * 
			 * @author 陈德基
			 * @time 2017/7/11 16:26
			 */

			Logger.t(TAG).i("有扩展控件");
			try {
				Class<IViewGenerator> clazz = (Class<IViewGenerator>)Class
						.forName("com.wanhelibs.custom_view.generatores." + genName);
				IViewGenerator viewGenerator = clazz.newInstance();
				generatorMap.put(genName, viewGenerator);
			} catch (ClassNotFoundException e) {
				Logger.t(TAG).e(e.getMessage());
			} catch (InstantiationException e) {
				Logger.t(TAG).e(e.getMessage());
			} catch (IllegalAccessException e) {
				Logger.t(TAG).e(e.getMessage());
			}

		}

		@Override
		public void onErr(Throwable throwable) {
			Logger.t(TAG).e(throwable.getMessage());
		}
	};

}
